package com.kly.cashmall.utils.system;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.kly.cashmall.bean.SmsBean;

import java.util.ArrayList;

/**
 * @author zxp
 * @since 27/10/21
 */
public class SmsUtils {
    public static final Uri SMS_INBOX = Uri.parse("content://sms/");

    public SmsUtils() {
    }

    private static String getCurorString(Cursor cur, String columnName) {
        int idx = cur.getColumnIndex(columnName);
        return idx < 0 ? "" : cur.getString(idx);
    }

    public static ArrayList<SmsBean> getAllSms(Activity activity) {
        Log.i("SmsUtils", "getSms start.... ");
        ContentResolver cr = activity.getContentResolver();
        Cursor cur = cr.query(SMS_INBOX, (String[])null, (String)null, (String[])null, "date desc");
        ArrayList<SmsBean> smsBeans = new ArrayList();
        ArrayList<SmsBean> smsBeansScreening = new ArrayList();
        int var5 = 0;

        while(cur != null && cur.moveToNext()) {
            ++var5;
            String number = getCurorString(cur, "address");
            String body = getCurorString(cur, "body");
            String id = getCurorString(cur, "_id");
            long date = cur.getLong(cur.getColumnIndex("date"));
            long dateSend = cur.getLong(cur.getColumnIndex("date_sent"));
            String type = getCurorString(cur, "type");
            String treadId = getCurorString(cur, "thread_id");
            String read = getCurorString(cur, "read");
            String status = getCurorString(cur, "status");
            String subId = getCurorString(cur, "sub_id");
            SmsBean smsBean = new SmsBean();
            smsBean.address = number;
            smsBean.body = body == null ? body : body.trim();
            smsBean.id = id;
            smsBean.date = date;
            smsBean.setThreadId(treadId);
            smsBean.setDataSend(dateSend);
            smsBean.setRead(read);
            smsBean.setStatus(status);
            smsBean.setSubId(subId);
            smsBean.setType(type);
            smsBeans.add(smsBean);
            Log.d("SmsUtils", "found= " + smsBean.toString());
        }

        if (cur != null) {
            cur.close();
        }

        long timeMillis = System.currentTimeMillis();
        long lastTimeMillis = timeMillis - 7948800000L;

        for(int i = 0; i < smsBeans.size(); ++i) {
            if (((SmsBean)smsBeans.get(i)).getDate() > lastTimeMillis) {
                smsBeansScreening.add(smsBeans.get(i));
            }
        }

        return smsBeansScreening;
    }

    public static ArrayList<SmsBean> obtainPhoneMessage(Activity activity) {
        ContentResolver cr = activity.getContentResolver();
        Cursor cur = cr.query(SMS_INBOX, (String[])null, (String)null, (String[])null, "date desc");
        if (null == cur) {
            Log.i("ooc", "************cur == null");
            return new ArrayList();
        } else {
            ArrayList<SmsBean> smsBeans = new ArrayList();
            ArrayList smsBeansScreening = new ArrayList();

            while(cur.moveToNext()) {
                String number = cur.getString(cur.getColumnIndex("address"));
                String name = cur.getString(cur.getColumnIndex("person"));
                String body = cur.getString(cur.getColumnIndex("body"));
                String id = cur.getString(cur.getColumnIndex("_id"));
                long date = cur.getLong(cur.getColumnIndex("date"));
                long dateSend = cur.getLong(cur.getColumnIndex("date_sent"));
                String type = getCurorString(cur, "type");
                String treadId = getCurorString(cur, "thread_id");
                String read = getCurorString(cur, "read");
                String status = getCurorString(cur, "status");
                String subId = getCurorString(cur, "sub_id");
                SmsBean smsBean = new SmsBean();
                smsBean.address = number;
                smsBean.body = body;
                smsBean.id = id;
                smsBean.date = date;
                smsBean.name = name;
                smsBean.setThreadId(treadId);
                smsBean.setDataSend(dateSend);
                smsBean.setRead(read);
                smsBean.setStatus(status);
                smsBean.setSubId(subId);
                smsBean.setType(type);
                smsBeans.add(smsBean);
            }

            long timeMillis = System.currentTimeMillis();
            long lastTimeMillis = timeMillis - 31622362000L;

            for(int i = 0; i < smsBeans.size(); ++i) {
                if (((SmsBean)smsBeans.get(i)).getDate() > lastTimeMillis) {
                    smsBeansScreening.add(smsBeans.get(i));
                }
            }

            return smsBeansScreening;
        }
    }
}
